<template>
  <el-dialog title="新建标签组" :visible.sync="$attrs.visableIf" @close="handleCloseDialog" :close-on-click-modal="false" :close-on-press-escape="false" append-to-body width="500px">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px" label-position="top" size="small">
<!--      行为类型只有行为才有，需要做判断-->
      <el-form-item label="行为类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分组名称" prop="labelGroupName">
        <el-input v-model="form.labelGroupName" placeholder="请输入标签组名称"></el-input>
      </el-form-item>
      <div class="com-title mb-5">标签名称：<span>（至少设置一个标签）</span></div>
      <el-form-item v-for="(item,index) in form.labelList" :prop="`labelList[${index}].name`"
        :rules="{required: true, message: '标签名称不能为空', trigger: 'blur'}" :key="index">
        <div class="item-big">
          <el-input v-model="item.name" placeholder="请输入标签名称"></el-input>
          <el-button icon="el-icon-delete"  @click="getDelLabel(index)" :disabled="form.labelList && form.labelList.length==1"></el-button>
        </div>
      </el-form-item>
      <el-button type="primary" plain size="small" @click="addLabel">新增标签</el-button>
    </el-form>
    <div class="footer-btn" slot="footer">
      <el-button type="primary" @click="handleCloseDialog">取消</el-button>
      <el-button @click="onSubmit('form')">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>


export default {
  name: 'addLabelGroup',

  components: {},
  data() {
    return {
      typeList:[
        {value:1,label:'社群行为',},
        {value:2,label:'客户行为',}
      ],
      form: {
        type:null,
        labelGroupName: null,//标签名称
        labelList: [{
          name:'',
        }],
      },
      rules: {
        'labelGroupName': [
          {required: true, message: '请输入标签组名称', trigger: 'change'}
        ],
        'type': [
          {required: true, message: '请选择行为类型', trigger: 'change'}
        ],
        'name': [
          {required: true, message: '请输入标签名称', trigger: 'blur'}
        ],

      },
    }
  },
  created() {
  },
  mounted() {
  },
  watch: {},
  methods: {
    getDelLabel(index){
      this.form.labelList.splice(index,1)
    },
    addLabel(){
      this.form.labelList.push({name:''});
    },
    onSubmit(formName) {
      //提交的代码
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('提交表单');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleCloseDialog() {
      this.$emit('update:visableIf', false);
      this.$emit('handleQuery');
    }
  }
}
</script>

<style scoped lang="scss">
.footer-btn {
  display: flex;
  justify-content: flex-end;
}

.item-li {
  padding: 16px;
  background: #f7f7f7;
  border-radius: 2px;

  .el-radio-group {
    margin-bottom: 10px;
  }
}

::v-deep .el-dialog__body {
  overflow-y: auto;
  min-height: 300px;
  max-height: 600px;
  padding: 20px;
  .el-form-item {
    border: none !important;
    margin-bottom: 20px !important;
    .el-form-item__label {
      padding-bottom: 0px
    }
    .item-big {
      display: flex;
       .el-button--default {
        padding: 0px 10px;
        margin-left: 20px;

      }
    }
  }
}
.com-title {
  font-size: 14px;
  padding-left: 12px;
  position: relative;
  &:after{
    content: '*';
    position: absolute;
    left: 2px;
    content: "*";
    color: #ff4949;
  }
  span {
    color: rgba(0, 0, 0, 0.45);
  }
}
</style>
